﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BySoft.Web.Utility;
using BySoft.Server.Common;
using BySoft.Server.Consts;
using BySoft.Server.Common.Entity;
using BySoft.Server.Business;
using BySoft.Server.Common.Enum;
using OneSoft.EntityCore;
using System.Web.UI.HtmlControls;
using ZCZP.Web.Site;

namespace BySoft.Web.Site.OutSource
{
    public partial class OutMatReturnAdd : AuthBasePage
    {
        #region 属性定义
        private string m_strsequenceName = "BsOutmatreturn" + DateTime.Now.ToString("yyyyMMdd");
        private string m_strTbl = "BS_OutMatReturn";
        private string m_strMainKey = "BillID";

        private DataTable DtOutMatReturnData
        {
            get
            {
                if (null == Session["OutMatReturnData"])
                {
                    return CreateDT();
                }
                else
                {
                    return (DataTable)Session["OutMatReturnData"];
                }
            }
            set
            {
                Session["OutMatReturnData"] = value;
            }
        }

        private DataTable CreateDT()
        {
           
            return  BsOutmatreturn.CreateDetailDT();
        }
        #endregion

        #region 初始化及绑定控件
        private void InitData()
        {
            DtOutMatReturnData = null;
            SetControlAttr(new BsOutmatreturnEntity(), tBase);
            lblBillID.Text = ClsComm.GetBillCode(m_strTbl, m_strMainKey, "WR" + DateTime.Now.ToString("yyMMdd"), 3);
            lblBillDate.Text = DateTime.Now.ToString("yyyy-MM-dd");

            UIHelper.BindEmptyGridView(grdResult, CreateDT());
        }
        #endregion

        #region 控件事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                grdResult.Style.Add("word-break", "keep-all");
                grdResult.Style.Add("word-wrap", "normal");
                InitData();
            }
            Master.ValidationGroup = "gSubmit";
            UIHelper.RunScript(this.UpdatePanel1, "Refreashtab();");
        }

       

        private void GetGridViewData()
        {
            DataTable dttemp = CreateDT();
            foreach (GridViewRow gvr in grdResult.Rows)
            {
                Label lblMaterialID = gvr.FindControl("lblMaterialID") as Label;
                if (lblMaterialID == null || string.IsNullOrEmpty(lblMaterialID.Text.Replace("&nbsp;", "")))
                {
                    continue;
                }

               
               // Label lblMaterialID = gvr.FindControl("lblMaterialID") as Label;
                Label lblOutOrderDetailID = gvr.FindControl("lblOutOrderDetailID") as Label;
                TextBox txtBackNum = gvr.FindControl("txtBackNum") as TextBox;
                TextBox txtReturnReason = gvr.FindControl("txtReturnReason") as TextBox;
                TextBox txtCompensateAmount = gvr.FindControl("txtCompensateAmount") as TextBox;


                DataRow row = dttemp.NewRow();
                row["MaterialID"] = GetGridViewString(lblMaterialID);
                row["Nums"] = GetGridViewString(txtBackNum);
                row["OutOrderDetailID"] = GetGridViewString(lblOutOrderDetailID);
                row["ReturnReason"] = GetGridViewString(txtReturnReason);
                row["CompensateAmount"] = GetGridViewString(txtCompensateAmount);
               

                dttemp.Rows.Add(row);
            }
            dttemp.AcceptChanges();
            DtOutMatReturnData = dttemp;
        }

        private void BindData()
        {
            if (DtOutMatReturnData.Rows.Count > 0)
            {
               
                grdResult.DataSource = DtOutMatReturnData;
                grdResult.DataBind();
                SetControlAttr(new BsOutmatreturndetailEntity(), grdResult);
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, CreateDT());
            }
            UIHelper.RunScript(this.UpdatePanel1, "iframeAutoFit();");
        }

      

       

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (!ValidateData())
            {
                return;
            }

            BsOutmatreturnEntity entity = new BsOutmatreturnEntity();
            entity.OutOrderID = GetString( txtOutOrderID);

            entity.WorkOrderNo = GetString(txtWorkOrderNo);
            entity.SupplierID = GetString(hidSupplierID);

            entity.BillDate = GetDateTime(lblBillDate);
            entity.BillID = GetString(lblBillID);
            entity.Status = "1";
            entity.CreateUserID = CurrentUser.UserID;
            entity.CreateUserName = CurrentUser.UserName;
            entity.CreateDate = DateTime.Now;
         
          



            BsOutmatreturn.SaveOrUpdate(entity, DtOutMatReturnData,true);
            base.LogAction(BaseEnum.ActionType.Add, entity.BillID, true, null, new EntityBase[] { entity });
            DtOutMatReturnData = null;
            UIHelper.AlertMessageGoToURL(this.Page, Message.SaveSuccess, "../OutOrder/OutMatReturnList.aspx");
        }

        protected void grdResult_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                string str = e.CommandArgument.ToString();
                int iDel = CommonMethod.ConvertToInt(str);
               // GetGridViewData();
                DataTable dt = DtOutMatReturnData.Copy();
                dt.Rows.RemoveAt(iDel);
                DtOutMatReturnData = dt.Copy();
                BindData();
                UIHelper.AlertMessage(this.Page, Message.DeleteSuccess);
            }
        }

        protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                #region 删除绑定
                ImageButton ibtn = (ImageButton)e.Row.FindControl("ibtnDel");
                Button btnDel = (Button)e.Row.FindControl("btnDel");
                if (ibtn != null)
                {
                    ibtn.OnClientClick = "ShowTopConfirm('提示信息','" + Message.ConfirmDelete +

        "','confirmCallback(\\'" + btnDel.ClientID + "\\')');return false;";
                }
                #endregion

              
            }
        }

      
             protected void btnSelectOutOrderID_Click(object sender, EventArgs e)
        {
            BindDetailData();

        }

        private void BindDetailData()
        {
            string strOutOrderID = hidOutOrderID.Value.Trim();

            if (string.IsNullOrEmpty(strOutOrderID))
            {
                return;
            }
            DataTable dtTemp = CreateDT();

            DataTable dt = BsOutmatreturn.SelectOutProcessDetail(lblBillID.Text, strOutOrderID);
            //UIHelper.AlertMessage(this.Page, strOutOrderID + dt.Rows.Count);

            if (dt.Rows.Count != 0)
            {

                DataRow dr = dt.Rows[0];
                txtWorkOrderNo.Text = CommonMethod.GetString(dr["WorkOrderNo"]);
                txtScheduleID.Text = CommonMethod.GetString(dr["ScheduleID"]);
                hidSupplierID.Value = CommonMethod.GetString(dr["SupplierID"]);
                txtSupplierName.Text = CommonMethod.GetString(dr["SupplierName"]);
                txtOutOrderID.Text = strOutOrderID;



                DtOutMatReturnData = CommonMethod.CopyDTtoDT(dt, dtTemp);

            }
            BindData();
        }

        #endregion

        #region 本地方法
        private bool ValidateData()
        {
            string strMention = "";


            if (ClsComm.CheckExist(m_strTbl, m_strMainKey, lblBillID.Text.Trim(), true))
                {
                    strMention = "委外件退料编号已经存在！<br />";
                }

                GetGridViewData();

                DataTable dt = DtOutMatReturnData;
                decimal sum = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    sum +=CommonMethod.ConvertToDecimal(dr["Nums"],2);
                }
                if (sum<=0)
                {
                    strMention = "退回数量不可为空！<br />";
                }
               
            if (string.IsNullOrEmpty(strMention))
            {
                return true;
            }
            else
            {
                UIHelper.AlertMessage(this.Page, strMention);
                return false;
            }
        }
        #endregion


    }
}

